//package cn.lili.controller.alipay;
//
//import cn.lili.common.enums.ResultCode;
//import cn.lili.common.exception.ServiceException;
//import cn.lili.common.properties.ApiProperties;
//import cn.lili.common.properties.DomainProperties;
//import cn.lili.common.utils.SnowFlake;
//import cn.lili.controller.wxpay.BaseController;
//import cn.lili.modules.payment.kit.plugin.alipay.AliPayApi;
//import cn.lili.modules.store.entity.dos.StoreZft;
//import cn.lili.modules.store.service.StoreZftService;
//import cn.lili.modules.system.entity.dos.Setting;
//import cn.lili.modules.system.entity.dto.payment.AlipayPaymentSetting;
//import cn.lili.modules.system.entity.enums.SettingEnum;
//import cn.lili.modules.system.service.SettingService;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONArray;
//import com.alibaba.fastjson.JSONObject;
//import com.alipay.api.domain.AlipayTradeMergePrecreateModel;
//import com.alipay.api.domain.OrderDetail;
//import com.alipay.api.domain.SubMerchant;
//import com.alipay.api.response.AlipayTradeMergePrecreateResponse;
//import com.alipay.api.response.AlipayTradeWapMergePayResponse;
//import com.jfinal.json.FastJson;
//import com.jfinal.kit.Ret;
//import com.jfinal.weixin.sdk.api.ApiResult;
//import com.jfinal.weixin.sdk.api.QrcodeApi;
//import lombok.SneakyThrows;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.collections4.CollectionUtils;
//import org.apache.commons.lang3.BooleanUtils;
//import org.apache.commons.lang3.ObjectUtils;
//import org.apache.commons.lang3.RandomStringUtils;
//import org.apache.commons.lang3.StringUtils;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.StringRedisTemplate;
//import org.springframework.http.MediaType;
//import org.springframework.stereotype.Controller;
//import org.springframework.web.bind.annotation.PathVariable;
//import org.springframework.web.bind.annotation.RequestMapping;
//import org.springframework.web.bind.annotation.RequestParam;
//import org.springframework.web.bind.annotation.ResponseBody;
//
//import javax.servlet.http.HttpServletRequest;
//import java.util.ArrayList;
//import java.util.List;
//import java.util.concurrent.TimeUnit;
//
//@SuppressWarnings("DuplicatedCode")
//@Slf4j
//@Controller
//@RequestMapping({"/buyer/alipay"})
//public class CreateOrderController extends BaseController {
//
//    @Autowired
//    private SettingService settingService;
//    @Autowired
//    private StoreZftService storeZftService;
//    @Autowired
//    private StringRedisTemplate stringRedisTemplate;
//
//    /**
//     * API域名
//     */
//    @Autowired
//    private ApiProperties apiProperties;
//    @Autowired
//    private DomainProperties domainProperties;
//
//    @RequestMapping("/{key}")
//    @ResponseBody
//    public Ret index(@PathVariable String key, HttpServletRequest request) throws Exception {
//        String content = stringRedisTemplate.opsForValue().get("wxpay:" + key);
//        if (StringUtils.isNotEmpty(content)) {
//            if (BooleanUtils.isNotTrue(stringRedisTemplate.hasKey("wx:pay:paid:" + key))) {
//                Ret param = FastJson.getJson().parse(content, Ret.class);
//                String type = param.getStr("type");
//                switch (type) {
//                    case "h5":
//                        String url = doH5(key, param, request);
//                        return Ret.ok("url", url).set("type", "h5");
//                    default:
//                        break;
//                }
//            } else {
//                return Ret.fail("code", "paid");
//            }
//
//        }
//        return Ret.fail("code", "404");
//    }
//
////
////    /**
////     * 退款
////     */
////    @RequestMapping(path = "/refund", produces = MediaType.APPLICATION_JSON_VALUE)
////    @ResponseBody
////    public Ret refund(@RequestParam String p, HttpServletRequest request) {
////        try {
////            p = base64Decode(StringUtils.trim(p));
////            String content = decrypt(StringUtils.trim(p));
////            log.info("退款内容：{}", content);
////            Ret param = FastJson.getJson().parse(content, Ret.class);
////            String orderId = param.getStr("orderId");
////            AlipayPaymentSetting setting = AlipayPaymentSetting();
////            JSONObject complaint = param.getAs("complaint");
////            JSONArray complaintOrderInfo = complaint.getJSONArray("complaint_order_info");
////            log.info("退款单数：{}", complaintOrderInfo.size());
////            for (Object o : complaintOrderInfo) {
////                JSONObject orderInfo = (JSONObject) o;
////                int amount = orderInfo.getInteger("amount");
////                Ret response = WechatV3Api.response(complaint.getString("complaint_id"), complaint.getString("complainted_mchid"));
////                log.info("回复用户结果：{}", response);
////                Ret updateRefundProgress = WechatV3Api.updateRefundProgress(complaint.getString("complaint_id"), "APPROVE");
////                log.info("更新投诉审批结果：{}", updateRefundProgress);
////                Ret refundParam = Ret.create().set("sp_appid", setting.getServiceAppId())
////                        .set("sub_mchid", complaint.getString("complainted_mchid"))
////                        .set("transaction_id", orderInfo.getString("transaction_id"))
////                        .set("out_refund_no", orderInfo.getString("out_trade_no"))
////                        .set("reason", "协商退款")
////                        .set("refund_account", StringUtils.defaultIfEmpty(param.getStr("refundAccount"), "REFUND_SOURCE_SUB_MERCHANT"))
////                        .set("notify_url", apiProperties.getBuyer() + "/buyer/payment/so/notify/WECHAT/refund")
////                        .set("amount", Ret.create().set("currency", "CNY").set("refund", amount).set("total", amount));
////                log.info("微信退款参数 {}", JSON.toJSONString(refundParam));
////                Ret result = WechatV3Api.refundApply(refundParam);
////                log.info("微信退款响应 {}", result);
////
////                //退款申请成功
////                if (result.isOk()) {
////                    Ret completeRefund = WechatV3Api.completeRefund(complaint.getString("complaint_id"), complaint.getString("complainted_mchid"));
////                    log.info("完成投诉处理：{}", completeRefund);
////                    log.info("\n{}退款成功 {} ", orderId, orderInfo.getString("out_trade_no"));
////                } else {
////                    log.info("\n{}退款失败 {} ", orderId, orderInfo.getString("out_trade_no"));
////                }
////            }
////            return Ret.ok();
////        } catch (Exception e) {
////            log.error("退款失败", e);
////            return (Ret.fail("msg", e.getMessage()));
////        }
////    }
//
////    /**
////     * 退款
////     */
////    @RequestMapping(path = "/refund1", produces = MediaType.APPLICATION_JSON_VALUE)
////    @ResponseBody
////    public Ret refund1(@RequestParam String p, HttpServletRequest request) {
////        try {
////            p = base64Decode(StringUtils.trim(p));
////            String content = decrypt(StringUtils.trim(p));
////            log.info("退款内容：{}", content);
////            Ret param = FastJson.getJson().parse(content, Ret.class);
////            String orderId = param.getStr("orderId");
////            double refundRate = ObjectUtils.defaultIfNull(param.getDouble("refundRate"), 1d);
////            AlipayPaymentSetting setting = AlipayPaymentSetting();
////            String c = stringRedisTemplate.opsForValue().get("wx:pay:paid:" + orderId);
////            log.info("退款账户：{}", param.getStr("refundAccount"));
////            if (StringUtils.isNotEmpty(c)) {
////                Ret orderInfo = FastJson.getJson().parse(c, Ret.class);
////                boolean isCombine = ObjectUtils.defaultIfNull(param.getBoolean("isCombine"), false);
////                if (isCombine) {
////                    JSONArray subOrders = orderInfo.getAs("sub_orders");
////                    int successNum = 0;
////                    for (Object subOrder : subOrders) {
////                        JSONObject obj = (JSONObject) subOrder;
////                        int totalAmount = obj.getJSONObject("amount").getIntValue("total_amount");
////                        int refundAmount = (int) (totalAmount * refundRate);
////                        String orderSuffix = "";
////                        if (MathUtils.equals(0.3, refundRate)) {
////                            refundAmount = (int) (totalAmount * 0.7);
////                            refundAmount = totalAmount - refundAmount;
////                            orderSuffix = "_3";
////                        } else if (MathUtils.equals(0.7, refundRate)) {
////                            orderSuffix = "_7";
////                        }
////                        Ret refundParam = Ret.create().set("sp_appid", setting.getServiceAppId())
////                                .set("sub_mchid", obj.getString("sub_mchid"))
////                                .set("transaction_id", obj.getString("transaction_id"))
////                                .set("out_refund_no", obj.getString("out_trade_no") + orderSuffix)
////                                .set("reason", "协商退款")
////                                .set("refund_account", StringUtils.defaultIfEmpty(param.getStr("refundAccount"), "REFUND_SOURCE_SUB_MERCHANT"))
////                                .set("amount", Ret.create().set("currency", "CNY").set("refund", refundAmount).set("total", totalAmount));
////                        log.info("微信退款参数 {}", JSON.toJSONString(refundParam));
////                        Ret result = WechatV3Api.refundApply(refundParam);
////                        log.info("微信退款响应 {}", result);
////                        if (result.isOk()) {
////                            successNum++;
////                        }
////                    }
////                    if (successNum == 0) {
////                        return Ret.fail("msg", "退款失败");
////                    } else if (successNum == subOrders.size()) {
////                        return Ret.ok("msg", "已全部退款");
////                    } else {
////                        return Ret.fail("msg", "部分退款，订单数量：" + subOrders.size() + "退款成功数量：" + successNum);
////                    }
////                } else {
////                    BigDecimal amount = BigDecimal.valueOf(Double.parseDouble(
////                            param.getStr("amount")));
////                    StoreSft sft = storeZftService.getByStoreId(param.getStr("storeId"));
////                    Ret refundParam = Ret.create().set("sp_appid", setting.getServiceAppId())
////                            .set("sub_mchid", sft.getSubMchid())
////                            .set("transaction_id", orderInfo.getStr("transaction_id"))
////                            .set("out_refund_no", orderInfo.getStr("out_trade_no"))
////                            .set("reason", "协商退款")
////                            .set("refund_account", StringUtils.defaultIfEmpty(param.getStr("refundAccount"), "REFUND_SOURCE_SUB_MERCHANT"))
////                            .set("amount", Ret.create().set("currency", "CNY").set("refund", amount.multiply(BigDecimal.valueOf(100L)).intValue()).set("total", amount.multiply(BigDecimal.valueOf(100L)).intValue()));
////                    log.info("微信退款参数 {}", JSON.toJSONString(refundParam));
////                    Ret result = WechatV3Api.refundApply(refundParam);
////                    log.info("微信退款响应 {}", result);
////
////                    return result;
////                }
////            }
////            return Ret.ok();
////        } catch (Exception e) {
////            log.error("退款失败", e);
////            return (Ret.fail("msg", e.getMessage()));
////        }
////    }
//
////    @RequestMapping(path = "/completeProfitsharing", produces = MediaType.APPLICATION_JSON_VALUE)
////    @ResponseBody
////    public Ret completeProfitsharing(@RequestParam String p, HttpServletRequest request) {
////        try {
////            p = base64Decode(StringUtils.trim(p));
////            String content = decrypt(StringUtils.trim(p));
////            log.info("微信解冻分账：{}", content);
////            Ret param = FastJson.getJson().parse(content, Ret.class);
////            String orderId = param.getStr("orderId");
////            String c = stringRedisTemplate.opsForValue().get("wx:pay:paid:" + orderId);
////            if (StringUtils.isNotEmpty(c)) {
////                Ret orderInfo = FastJson.getJson().parse(c, Ret.class);
////                StoreZft sft = storeZftService.getByStoreId(param.getStr("storeId"));
////
////                Ret result = WechatV3Api.completeProfitsharing(sft.getSubMchid(), orderInfo.getStr("transaction_id"), orderInfo.getStr("out_trade_no"), "完成分账");
////                log.info("微信解冻分账响应 {}", result);
////
////                return result;
////            }
////            return Ret.ok();
////        } catch (Exception e) {
////            log.error("退款失败", e);
////            return (Ret.fail("msg", e.getMessage()));
////        }
////    }
//
//    /**
//     * 预处理
//     */
//    @RequestMapping(path = "/pre", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ResponseBody
//    public Ret pre(@RequestParam String p, HttpServletRequest request) {
//        try {
//            p = base64Decode(StringUtils.trim(p));
//            String content = decrypt(StringUtils.trim(p));
//            Ret param = FastJson.getJson().parse(content, Ret.class);
//            String type = param.getStr("type");
//            log.info("预下单请求内容2：{}", content);
//
//            String key = RandomStringUtils.randomAlphanumeric(10);
//
//            String outTradeNo = SnowFlake.getIdStr();
//            stringRedisTemplate.opsForValue().set("wxpay:" + key, param.set("key", key).set("outTradeNo", outTradeNo).toJson(), 600, TimeUnit.SECONDS);
//            stringRedisTemplate.opsForValue().set("wxpay:" + param.getStr("orderId"), param.set("key", key).set("outTradeNo", outTradeNo).toJson(), 30, TimeUnit.DAYS);
//            JSONArray orders = param.getAs("orders");
//            if (CollectionUtils.isNotEmpty(orders)) {
//                for (Object order : orders) {
//                    JSONObject obj = (JSONObject) order;
//                    stringRedisTemplate.opsForValue().set("wxpay:idref:" + obj.getString("outTradeNo"), param.getStr("orderId"), 30, TimeUnit.DAYS);
//                }
//            }
//
//            log.info("\n生成预下单地址:{}", param.toJson());
//            log.info("\n生成预下单地址成功:{}", getBaseUrl(request) + "/wx/user/order/" + key);
//            AlipayPaymentSetting setting = AlipayPaymentSetting();
//
//            Ret result = Ret.ok("url", domainProperties.getWap() + "/pages/go/index?key=" + key);
//            switch (type) {
//                case "h5":
//                    result.set("type", "link");
//                    //String url = doH5(param, request);
//                    return result;
//                case "native":
//                    String url = doNative(key, param, request);
//                    return Ret.ok("url", url).set("type", "qr");
//                case "mpn":
//                    ApiResult temporary = QrcodeApi.createTemporary(3 * 60, key);
//                    url = temporary.get("url");
//                    return Ret.ok("url", url).set("type", "qr");
//                default:
//                    break;
//            }
//        } catch (Exception e) {
//            log.error("生成预下单地址失败", e);
//            return (Ret.fail("msg", e.getMessage()));
//        }
//        return Ret.fail();
//    }
//
//    @SneakyThrows
//    public String preCreate(String key, Ret param, String productCode, HttpServletRequest request) {
//        String orderId = param.getStr("orderId");
//        String redisPayInfoKey = "wxpay:payInfo:" + orderId;
//        boolean isCombine = ObjectUtils.defaultIfNull(param.getBoolean("isCombine"), false);
//        String preOrderNo = null;
//
//        if (BooleanUtils.isNotTrue(stringRedisTemplate.hasKey(redisPayInfoKey))) {
//            AlipayPaymentSetting setting = AlipayPaymentSetting();
//            String outTradeNo = param.getStr("outTradeNo");
//
//            if (isCombine) {
//
//                AlipayTradeMergePrecreateModel model = new AlipayTradeMergePrecreateModel();
//                model.setTimeoutExpress("5m");
//                model.setOutMergeNo(outTradeNo);
//
//                JSONArray orders = param.getAs("orders");
//                List<OrderDetail> orderDetails = new ArrayList<>();
//                orders.stream().map(o -> (JSONObject) o).forEach(o -> {
//                    StoreZft zft = storeZftService.getByStoreId(o.getString("storeId"));
//                    OrderDetail detail = new OrderDetail();
//                    detail.setAppId(setting.getAppId());
//                    detail.setOutTradeNo(o.getString("outTradeNo"));
//                    detail.setProductCode(productCode);
//                    detail.setTotalAmount(o.getString("preAmount"));
//                    detail.setSubject("在线订单支付");
//                    SubMerchant subMerchant = new SubMerchant();
//                    subMerchant.setMerchantId(zft.getSmid());
//
//                    detail.setSubMerchant(subMerchant);
//                    orderDetails.add(detail);
//                });
//                model.setOrderDetails(orderDetails);
//                log.info("支付宝合单参数：{}", JSON.toJSONString(model));
//                AlipayTradeMergePrecreateResponse response = AliPayApi.tradeMergePreCreateToResponse(model, apiProperties.getBuyer() + "/buyer/payment/so/notify/ALIPAY", null);
//                log.info("支付宝合单结果：{}", JSON.toJSONString(response));
//                if (response.isSuccess()) {
//                    preOrderNo = response.getPreOrderNo();
//                }
//            } else {
//
////                StoreZft zft = storeZftService.getByStoreId(param.getStr("storeId"));
////                AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
////                model.setTimeoutExpress("5m");
////                model.setOutTradeNo(outTradeNo);
////                model.setProductCode(productCode);
////                model.setTotalAmount(param.getStr("preAmount"));
////                model.setSubject("在线订单支付");
////
////
////                AliPayApi.tradePrecreatePayToResponse(model, apiProperties.getBuyer() + "/buyer/payment/so/notify/ALIPAY");
////
////                params = Ret.create()
////                        .set("sp_appid", setting.getServiceAppId())
////                        .set("sp_mchid", setting.getMchId())
////                        .set("sub_mchid", zft.getSubMchid())
////                        .set("description", "在线支付")
////                        .set("out_trade_no", outTradeNo)
////                        .set("time_expire", timeExpire)
////                        .set("attach", Base64.getEncoder().encodeToString(Ret.create("orderId", orderId).set("key", key).toJson().getBytes(StandardCharsets.UTF_8)))
////                        .set("notify_url", apiProperties.getBuyer() + "/buyer/payment/so/notify/WECHAT")
////                        .set("amount", Ret.create("total", amount))
////                        .set("scene_info", Ret.create("payer_client_ip", IpUtils.getIpAddress(request)).set("h5_info", Ret.create("type", "Wap")));
////
////                //是否分账
////                if ("1".equals(setting.getIsProfitSharing())) {
////                    Ret profitSharing = Ret.create("profit_sharing", true);
////                    Integer subsidyAmount = param.getInt("subsidy_amount");
////                    if (subsidyAmount != null && subsidyAmount > 0) {
////                        profitSharing.set("subsidy_amount", subsidyAmount);
////                    }
////                    params.set("settle_info", profitSharing);
////                }
//            }
//
//
//            stringRedisTemplate.opsForValue().set("wxpay:idref:" + orderId, outTradeNo);
//            stringRedisTemplate.opsForValue().set("wxpay:notify:" + outTradeNo, param.getStr("notifyUrl"), 86400, TimeUnit.SECONDS);
//            stringRedisTemplate.opsForValue().set(redisPayInfoKey, preOrderNo, 86400, TimeUnit.SECONDS);
//        } else {
//            preOrderNo = stringRedisTemplate.opsForValue().get(redisPayInfoKey);
//        }
//
//        return preOrderNo;
//    }
//
//    public String doH5(String key, Ret param, HttpServletRequest request) throws Exception {
//        String preOrderNo = preCreate(key, param, "QUICK_MSECURITY_PAY", request);
//        AlipayTradeWapMergePayResponse response = AliPayApi.tradeWapMergePayToResponse(preOrderNo, param.getStr("notifyUrl"), null);
//        return response.getBody();
//    }
//
//    public String doNative(String key, Ret param, HttpServletRequest request) throws Exception {
//        return null;
//    }
//
//    private AlipayPaymentSetting AlipayPaymentSetting() {
//        try {
//            Setting systemSetting = settingService.get(SettingEnum.ALIPAY_PAYMENT.name());
//            return JSON.parseObject(systemSetting.getSettingValue()).toJavaObject(AlipayPaymentSetting.class);
//        } catch (Exception e) {
//            log.error("微信支付暂不支持", e);
//            throw new ServiceException(ResultCode.PAY_NOT_SUPPORT);
//        }
//    }
//}
